package ca.kooki.codesnippet.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;

import ca.kooki.codesnippet.Activator;
import ca.kooki.codesnippet.Tag;
import ca.kooki.codesnippet.core.SystemDate;
import ca.kooki.codesnippet.core.User;

public class CodeSnippetSearchViewHandler implements SelectionListener {

  //```````````````````````
  // MEMBER VARIABLES
  //```````````````````````

  public Text inputSearch;
  public Composite outputResults;
  public Button buttonSearch;
  
  private Font _codeFont; 
  
  //```````````````````````
  // CONSTRUCTOR
  //```````````````````````

  public CodeSnippetSearchViewHandler(Text inputSearch, Composite outputResults,Button buttonSearch)
  {
    this.outputResults = outputResults;
    this.buttonSearch = buttonSearch;
    this.inputSearch = inputSearch;
    _codeFont = new Font(null,new FontData("Courier New", 10, SWT.NORMAL));
  }
  
  //```````````````````````
  // INTERFACE
  //```````````````````````

  public Composite createTagComposite(Composite parent, Tag tag)
  {
    Composite tagArea = new Composite(parent,SWT.NULL);
    
    GridData data = new GridData();
    tagArea.setLayoutData(data);
    
    GridLayout layout = new GridLayout();
    layout.numColumns = 1;
    tagArea.setLayout(layout);
    
    String editOrTag = tag.getUser().equals(Activator.getDefault().getRepository().getUser()) ? "edit" : "tag";
    
    String titleText = tag.getTitle() + " <a>show</a> <a>"+ editOrTag +"</a>";
    String keywordsText = "to";
    
    String[] allKeywords = tag.getKeywordList();
    for (int i=0; i<allKeywords.length; i++)
    {
      keywordsText += " <a>" + allKeywords[i] + "</a>";
    }
    keywordsText += " on " + SystemDate.formatDate(tag.getUpdated());
    
    Link linkTitle = new Link(tagArea, SWT.LEFT);
    linkTitle.setText(titleText);
    linkTitle.setData(tag);
    linkTitle.addSelectionListener(this);

    Link linkKeywords = new Link(tagArea, SWT.LEFT);
    linkKeywords.setText(keywordsText);
    linkKeywords.setData(tag.getUser());
    linkKeywords.addSelectionListener(this);
    
    return tagArea;
  }

  public void widgetDefaultSelected(SelectionEvent e) {
    widgetSelected(e);
  }

  public void widgetSelected(SelectionEvent e) {

    if (e.widget.getClass().equals(Button.class))
    {
      searchTags();
    }
    else if (e.widget.getClass().equals(Link.class))
    {
      if(e.text.equals("show"))
      {
        toggleCode(e);
      }
      else if (e.text.equals("tag") || e.text.equals("edit"))
      {
        TagEntryView view = Activator.getDefault().showTagEntryView();
        Link link = (Link)e.widget;
        Tag tag = (Tag)link.getData();
        
        if (tag.getUser().equals(Activator.getDefault().getRepository().getUser()))
        {
          view.updateTag(tag);
        }
        else
        {
          view.tagExistingCodeSnippet(tag);  
        }
      }
      else
      {
        searchKeyword(e);
      }
    }
    resizeOutputs();
  }

  public void resizeOutputs()
  {
    outputResults.layout(true,true);
    ScrolledComposite scrolled = (ScrolledComposite)outputResults.getParent();
    scrolled.setMinSize(outputResults.computeSize(SWT.DEFAULT, SWT.DEFAULT));
  }

  
  //```````````````````````
  // PRIVATE METHODS
  //```````````````````````

  private void searchTags()
  {
    Control[] staleResults = outputResults.getChildren();
    for (int i=0; i<staleResults.length; i++)
    {
      staleResults[i].dispose();
    }
    
    User user = Activator.getDefault().getRepository().getUser();
    
    String rawSearch = inputSearch.getText();
    String searchTerms = rawSearch.equals("") ? "/" + user.getName() : rawSearch;
    Tag[] allTags = Activator.getDefault().getRepository().getAllTags(searchTerms);
    
    if (allTags.length == 0)
    {
      createLabel(outputResults, "No results found, sorry!");
      return;
    }
    
    for(int i=0; i<allTags.length; i++)
    {
      createTagComposite(outputResults,allTags[i]);
    }
    
  }

  private void searchKeyword(SelectionEvent event)
  {
    Link link = (Link)event.widget;
    User user = (User)link.getData();
    String newSearch = "/" + user.getName() + "/" + event.text;
    inputSearch.setText(newSearch);
    searchTags();
  }
  
  private void toggleCode(SelectionEvent event)
  {
    Link link = (Link)event.widget;
    Composite tagComposite = link.getParent();
    Control[] tagElements = tagComposite.getChildren();
    if (tagElements.length == 3)
    {
      tagElements[2].dispose();
    }
    else
    {
      Tag tag = (Tag)link.getData();
      String code = tag.getCodeSnippet().getCode();
      Text outputCode = new Text(tagComposite,SWT.MULTI | SWT.BORDER);
      outputCode.setText(code);
      outputCode.setFont(_codeFont);
      GridData data = new GridData(GridData.FILL_HORIZONTAL);
      outputCode.setLayoutData(data);
    }
  }
  
  private Label createLabel(Composite parent, String text)
  {
    Label myLabel = new Label(parent, SWT.LEFT);
    myLabel.setText(text);
    return myLabel;
  }

}
